﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SqlServerDriver
{
	/*
		Class to hold constants
		SqlCommand is taken by ADO
	*/
	class Cmd
	{
		// [Specialise: define extension commands]
		public const String TableSizes = "TABLESIZES";
		public const String Descriptions = "DESCRIPTION";
		public const String EditDescriptions = "EDITDESCRIPTION";
		public const String Articles = "ARTS";
		public const String CopyTable = "COPYTABLE";
		public const String CopyView = "COPYVIEW";
		public const String CopyProc = "COPYPROC";
		public const String Owners = "OWNERS";
		public const String Processes = "PROCESSES";
		public const String Locks = "LOCKS";
		public const String Jobs = "JOBS";
		public const String JobSteps = "JOBSTEPS";
		public const String JobHistory = "JOBHIST";
		public const String Publications = "PUBS";
		public const String ServiceBroker = "BROKER";
		public const String DependencyTree = "DEPTREE";
		public const String LastProcessCommand = "LASTSQL";
		public const String ScriptLinq = "DOLINQ";
		public const String ScriptClass = "ORM";
		public const String ScriptCompare = "COMP";
		public const String Users = "USERS";
		public const String LinkedServers = "LINKSRV";
		public const String QuickDiffDialog = "QUICKDIFFDLG";
		public const String DisplayHeldTable = "DISPLAYTABLE"; // this may be moved to the main framework
		public const String LaunchProfiler = "PROFILER"; // Launch the express profiler application
		public const String ExtendedProperties = "EXTPROP"; 
		public const String PropertyValues = "EXTPROPVAL"; 
		public const String DropTableProperty = "EXTPROP_DROPTABLE"; 
		public const String DropColumnProperty = "EXTPROP_DROPCOL"; 
		public const String ScriptAddTableProperty = "EXTPROP_ADDTABLE"; 
		public const String ScriptAddAllTableProperties = "EXTPROP_ADDTABLES"; 
		public const String ScriptAddTableColumnProperty = "EXTPROP_ADDTABLECOL"; 
		public const String ScriptAddAllColumnProperties = "EXTPROP_ADDALLCOLS"; 

		// "dangerous" commands
		public const String CreateTable = "CREATETABLE";
		public const String ResetAuto = "RESETAUTO";
		public const String AddFK = "ADDFK";
		public const String DelFK = "DELFK";
		public const String DisableFK = "DISABLEFK";
		public const String EnableFK = "ENABLEFK";
		public const String CreateSchema = "CREATESCHEMA";
		public const String RemoveSchema = "KILLSCHEMA";
		public const String TransferObjectToSchema = "XFERTOSCHEMA";

		// service broker commands
		public const String BrokerMessages = "SB_MESSAGES";
		public const String BrokerQueues = "SB_QUEUES";
		public const String BrokerContracts = "SB_CONTRACTS";
		public const String BrokerServices = "SB_SERVICES";
		public const String BrokerRoutes = "SB_ROUTES";
		public const String BrokerEndpoints = "SB_ENDPOINTS";
		public const String BrokerXmitQueue = "SB_XMITQUEUE";
		public const String BrokerDatabases = "SB_DBLIST";
		public const String BrokerQueueItems = "SB_QDATA";
		public const String BrokerQueueItemsNoData = "SB_QXDATA";
		// broker scripting
		public const String BrokerScriptDropMessage = "SB_DROPMSG";
		public const String BrokerScriptDropContract = "SB_DROPCONTRACT";
		public const String BrokerScriptDropService = "SB_DROPSERVICE";
		public const String BrokerScriptDropQueue = "SB_DROPQUEUE";
		public const String BrokerScriptEndConversation = "SB_ENDCONV";
		public const String BrokerScriptToggleActivation = "SB_TOGACT";
		public const String BrokerScriptToggleStatus = "SB_TOGSTAT";
		// broker object creation 
		public const String BrokerBuilderDialog = "SB_BUILDER";

		// builder. creates a custom command and sets the action
		public static DbView.Command MakeCommand(String commandString)
		{
			// default is to make an extension command that populates a table
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.Extension, commandString, DbView.Command.CommandAction.poptable);
			// [Specialise: override extension command action ]
			if (command.Name == Cmd.EditDescriptions ||
				command.Name == Cmd.CopyTable ||
				command.Name == Cmd.CopyView ||
				command.Name == Cmd.CopyProc ||
				command.Name == Cmd.QuickDiffDialog ||
				command.Name == Cmd.CreateTable ||
				command.Name == Cmd.ResetAuto ||
				command.Name == Cmd.ScriptClass ||
				command.Name == Cmd.BrokerBuilderDialog ||
				command.Name == Cmd.LaunchProfiler ||
				command.Name == Cmd.CreateSchema ||
				command.Name == Cmd.RemoveSchema ||
				command.Name == Cmd.AddFK ||
				command.Name == Cmd.DelFK ||
				command.Name == Cmd.DisableFK ||
				command.Name == Cmd.EnableFK
				)
				command.Action = DbView.Command.CommandAction.driveraction;
			if (command.Name == DependencyTree ||
				command.Name == LastProcessCommand ||
				command.Name == Cmd.ScriptLinq ||
				command.Name == Cmd.ScriptAddAllColumnProperties ||
				command.Name == Cmd.ScriptAddAllTableProperties ||
				command.Name == Cmd.ScriptAddTableColumnProperty ||
				command.Name == Cmd.ScriptAddTableProperty ||
				command.Name == Cmd.ScriptCompare ||
				command.Name == Cmd.DropTableProperty ||
				command.Name == Cmd.DropColumnProperty ||
				SqlServerBrokerPopupBuilder.IsBrokerScriptingCommand(command)
				)
				command.Action = DbView.Command.CommandAction.text;
			
			return command;
		}

		public static void ResetHelp(DbView.CommandHelpRepository helpRepository)
		{
			helpRepository.SetHelp(MakeCommand(TableSizes), "Show the physical disc size of the tables");
			helpRepository.SetHelp(MakeCommand(Owners), "Display schemas and database principals that own objects in the database");
			helpRepository.SetHelp(MakeCommand(Descriptions), "Display the Field description property values");
			helpRepository.SetHelp(MakeCommand(ScriptLinq), "Create a CSharp class from the table for access by Linq");
			helpRepository.SetHelp(MakeCommand(CopyTable), "Copy the table and contents to another table or another database");
			helpRepository.SetHelp(MakeCommand(CopyView), "Copy the view to another database");
			helpRepository.SetHelp(MakeCommand(CopyProc), "Copy the stored procedure to another database");
			helpRepository.SetHelp(MakeCommand(EditDescriptions), "Add or modify the description of the field.");
			helpRepository.SetHelp(MakeCommand(Articles), "Display the tables replicated in this publication");
			helpRepository.SetHelp(MakeCommand(Processes), "Display the processes running on the server of the current connection.");
			helpRepository.SetHelp(MakeCommand(Locks), "Display the locks on objects on the server of the current connection.");
			helpRepository.SetHelp(MakeCommand(JobSteps), "Display the commands that make up the job");
			helpRepository.SetHelp(MakeCommand(JobHistory), "Display details of when the job was run and the results.");
			helpRepository.SetHelp(MakeCommand(Publications), "Display replication publications set-up on the database");
			helpRepository.SetHelp(MakeCommand(ServiceBroker), "Display an overview of the service broker messages on this database");
			helpRepository.SetHelp(MakeCommand(DependencyTree), "Show all objects that the current on depends on both direct and indirect");
			helpRepository.SetHelp(MakeCommand(LastProcessCommand), "Attempt to discover the last Sql command run by the process");
			helpRepository.SetHelp(MakeCommand(QuickDiffDialog), "Find differences in tables, views or procs between this database and another.");
			helpRepository.SetHelp(MakeCommand(ResetAuto), "Check and reset the auto-increment value.");
			helpRepository.SetHelp(MakeCommand(AddFK), "Add a foreign key using the current field");
			helpRepository.SetHelp(MakeCommand(DelFK), "Remove the current foreign key");
			helpRepository.SetHelp(MakeCommand(DisableFK), "Remove integrity checking from the foreign key in effect disabling it.");
			helpRepository.SetHelp(MakeCommand(QuickDiffDialog), "Set or restore itegrity checking on the foreign key");
			helpRepository.SetHelp(MakeCommand(ScriptClass), "Create a POCO class from the table for use with an ORM framework");
			helpRepository.SetHelp(MakeCommand(ScriptCompare), "Script a query to compare every column in the table");
			helpRepository.SetHelp(MakeCommand(TransferObjectToSchema), "Move objects out of the selected schema");
			helpRepository.SetHelp(MakeCommand(RemoveSchema), "Delete the scheme (if it has no objects associated with it");
			helpRepository.SetHelp(MakeCommand(CreateSchema), "Add a new schema to the current database");
			helpRepository.SetHelp(MakeCommand(DropColumnProperty), "Delete the column properties of the currently selected table");
			helpRepository.SetHelp(MakeCommand(DropTableProperty), "Delete the property of the currently selected table");
			helpRepository.SetHelp(MakeCommand(PropertyValues), "Show the values of the selected property for each item of the selected type");
			helpRepository.SetHelp(MakeCommand(ScriptAddAllColumnProperties), "Script to the column extended properties of all the tables.");
			helpRepository.SetHelp(MakeCommand(ScriptAddAllTableProperties), "Script the extended properties of all the tables.");
			helpRepository.SetHelp(MakeCommand(ScriptAddTableColumnProperty), "Script the column properties for the table of the currently selected line.");
			helpRepository.SetHelp(MakeCommand(ScriptAddTableProperty), "Script the property of the currently selected table.");
		}
	}
	class CmdParam
	{
		// [Specialise: add defined parameters ]
		public const String Publication = "PUB";
		public const String JobId = "JOBID";
		public const String TableId = "TID";
		public const String FkName = "FKNAME";
		public const String FkTable = "FKTABLE";
		public const String PkTable = "PKTABLE";
		public const String FkField = "FKFIELD";
		public const String PkField = "PKFIELD";
		// service broker params
		public const String ConversationHandle = "CONV_HDL";
		public const String ContractName = "CONTRACT";
		public const String ServiceName = "SERVICE";
		public const String QueueName = "QUEUE";
		public const String ActivationFlag = "ACT";
		public const String StatusFlag = "STAT";
		public const String ProcedureName = "PROCNAME";
	}
}
